package com.changxianggu.cloud.edu.tool.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.edu.tool.constant.DbConstant;
import com.changxianggu.cloud.edu.tool.core.HandlerResultData;
import com.changxianggu.cloud.edu.tool.constant.HttpConstant;
import com.changxianggu.cloud.edu.tool.entity.Batch;
import com.changxianggu.cloud.edu.tool.entity.gen.GenCodeVO;
import com.changxianggu.cloud.edu.tool.entity.School;
import com.changxianggu.cloud.edu.tool.utils.ConfigUtil;
import com.changxianggu.cloud.edu.tool.utils.DialogUtils;
import com.changxianggu.cloud.edu.tool.view.AlertUtil;
import com.changxianggu.cloud.edu.tool.view.AutoCompleteComboBoxListener;
import com.changxianggu.cloud.edu.tool.view.ProgressFrom;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;
import java.text.Collator;
import java.util.*;

/**
 * @author liuyuantao
 */
@Slf4j
public class ExcelController extends BaseController {


    @FXML
    private TextField configPathField;

    @FXML
    private Button handlerBtn;

    @FXML
    private Button choseCollegeBtn;

    @FXML
    private Button choseOfficeBtn;

    @FXML
    private Button choseMajorBtn;

    @FXML
    private Button choseClazzBtn;

    @FXML
    private Button choseStudentBtn;

    @FXML
    private Button choseTeacherBtn;

    @FXML
    private Button choseCourseBtn;

    @FXML
    private Label collegeFilePath;

    @FXML
    private Label officeFilePath;

    @FXML
    private Label majorFilePath;

    @FXML
    private Label clazzFilePath;
    @FXML
    private Label studentFilePath;

    @FXML
    private Label teacherFilePath;

    @FXML
    private Label courseFilePath;

    @FXML
    private TextField headerRowIndex;

    @FXML
    private ComboBox schoolComboBox;

    @FXML
    private ComboBox batchComboBox;

    @FXML
    private CheckBox downAllCheckBox;
    @FXML
    private Button choseCourseStuBtn;

    @FXML
    private Label courseStuFilePath;

    @FXML
    private CheckBox genCollegeCodeBox;

    @FXML
    private CheckBox genOfficeCodeBox;

    @FXML
    private CheckBox genMajorCodeBox;

    @FXML
    private CheckBox genClazzCodeBox;

    @FXML
    private Button choseConfigBtn;

    private File excelRootDir;

    ProgressFrom progressFrom = null;

    private Integer schoolId = 0;
    private Integer batchId = 0;

    private Map<String, Integer> schoolMap = new HashMap<>();
    private Map<String, Integer> batchMap = new HashMap<>();

    private List<File> collegeFileList;
    private List<File> majorFileList;
    private List<File> officeFileList;
    private List<File> clazzFileList;

    private List<File> studentFileList;
    private List<File> teacherFileList;
    private List<File> courseFileList;
    private List<File> courseStuFileList;

    /**
     * 选择单个文件
     *
     * @param stage
     * @return
     */
    private File openChoseFile(Stage stage) {
        FileChooser fileChooser = new FileChooser();
        return fileChooser.showOpenDialog(stage);
    }

    /**
     * 选择多个文件
     *
     * @param stage
     * @return
     */
    private List<File> openMultiFile(Stage stage) {
        FileChooser fileChooser = new FileChooser();
        if (excelRootDir != null) {
            fileChooser.setInitialDirectory(excelRootDir);
        }
        return fileChooser.showOpenMultipleDialog(stage);

    }

    /**
     * 将文件的名字使用逗号进行拼接
     *
     * @param fileList
     * @return
     */
    private String joinFileName(List<File> fileList) {
        String result = "";
        if (CollUtil.isNotEmpty(fileList)) {
            List<String> strList = new ArrayList<>();
            for (File f : fileList) {
                strList.add(f.getName());
            }
            result = StrUtil.join(",", strList);
        }
        if (result.length() > 14) {
            result = "共" + fileList.size() + "个文件";
        }
        return result;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initExcelDir();
        initDownAll();
        String configPathStr = "";
        Entity entity = ConfigUtil.selectEntity(DbConstant.CONFIG_FILE_PATH_NAME);
        if (entity != null) {
            configPathStr = entity.getStr(DbConstant.TABLE_VALUE_FIELD);
            if (StrUtil.isNotBlank(configPathStr)) {
                if (!FileUtil.exist(configPathStr)) {
                    ConfigUtil.deleteEntity(DbConstant.CONFIG_FILE_PATH_NAME);
                    DialogUtils.showAlert("配置文件设置提示",
                            "配置文件路径不正确,请点击右侧选择配置文件按钮重新设置配置文件",
                            Alert.AlertType.INFORMATION);
                }
            }
        }
        //设置excel读取的行
        headerRowIndex.setText(this.getReadStartNum());
        configPathField.setText(configPathStr);
        configPathField.setEditable(false);
        this.initSchoolAndBatch();
        //初始化学校
        new AutoCompleteComboBoxListener<>(schoolComboBox);
        schoolComboBox.setVisibleRowCount(10);
        schoolComboBox.getEditor().setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Object oldValue = schoolComboBox.getValue();
                schoolComboBox.setValue("");
                schoolComboBox.getItems().add(oldValue);
            }
        });
        //初始化学期
        new AutoCompleteComboBoxListener<>(batchComboBox);
        batchComboBox.setVisibleRowCount(10);
        batchComboBox.getEditor().setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Object oldValue = batchComboBox.getValue();
                batchComboBox.setValue("");
                batchComboBox.getItems().add(oldValue);
            }
        });
        initSchoolId();
    }

    /**
     * 初始化学校名字
     */
    private void initSchoolAndBatch() {
        /**
         * 初始化学校列表
         */
        List<String> schoolNameList = getHttpSchoolList();
        //学校名字按拼音排序
        schoolNameList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Collator.getInstance(Locale.CHINESE).compare(o1, o2);
            }
        });
        //初始化学校名字
        String dbSchoolName = getSchoolName();
        if (schoolNameList.contains(dbSchoolName)) {
            schoolNameList.remove(dbSchoolName);
        }
        ObservableList<String> options = FXCollections.observableArrayList(schoolNameList);
        this.schoolComboBox.setItems(options);
        this.schoolComboBox.setValue(dbSchoolName);

        /**
         * 初始化学期
         */
        List<String> batchNameList = getHttpBatchList();
        //学校名字按拼音排序
        batchNameList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Collator.getInstance(Locale.CHINESE).compare(o1, o2);
            }
        });

        ObservableList<String> batchOptions = FXCollections.observableArrayList(batchNameList);
        this.batchComboBox.setItems(batchOptions);
    }

    /**
     * 从数据库读取schoolId
     */
    private void initSchoolId() {
        schoolId = 0;
        Entity entity = ConfigUtil.selectEntity(DbConstant.SCHOOL_ID_NAME);
        if (entity != null) {
            schoolId = entity.getInt(DbConstant.TABLE_VALUE_FIELD);
        }
    }

    /**
     * 获取存储的学校名字
     *
     * @return
     */
    private String getSchoolName() {
        String schoolName = "未上线";
        Entity entity = ConfigUtil.selectEntity(DbConstant.SCHOOL_NAME);
        if (entity != null) {
            String str = entity.getStr(DbConstant.TABLE_VALUE_FIELD);
            if (StrUtil.isNotBlank(str)) {
                schoolName = str;
            }
        }
        return schoolName;
    }

    /**
     * 获取excel读取的开始行
     *
     * @return
     */
    public String getReadStartNum() {
        String startNum = "1";
        Entity entity = ConfigUtil.selectEntity(DbConstant.READ_START_LINE_NUM_NAME);
        if (entity != null) {
            String str = entity.getStr(DbConstant.TABLE_VALUE_FIELD);
            if (StrUtil.isNotBlank(str)) {
                startNum = str;
            }
        }
        return startNum;
    }

    /**
     * 初始化excel的根目录
     */
    public void initExcelDir() {
        Entity selectEntity = ConfigUtil.selectEntity(DbConstant.EXCEL_FILE_DIR_NAME);
        if (selectEntity == null) {
            return;
        }
        String entityStr = selectEntity.getStr(DbConstant.TABLE_VALUE_FIELD);
        if (StrUtil.isNotBlank(entityStr)) {
            boolean exist = FileUtil.exist(entityStr);
            if (exist) {
                excelRootDir = FileUtil.file(entityStr);
            } else {
                ConfigUtil.deleteEntity(DbConstant.EXCEL_FILE_DIR_NAME);
            }
        }
    }

    public void initDownAll() {
        Entity selectEntity = ConfigUtil.selectEntity(DbConstant.IS_DOWN_ALL_NAME);
        if (selectEntity == null) {
            return;
        }
        int isDownAll = selectEntity.getInt(DbConstant.TABLE_VALUE_FIELD);
        if (isDownAll == 1) {
            downAllCheckBox.setSelected(true);
        }
    }

    /**
     * 获取学校列表数据
     *
     * @return
     */
    public List<String> getHttpSchoolList() {
        schoolMap.clear();
        String url = HttpConstant.SCHOOL_URL;
        HttpResponse execute = null;
        try {
            execute = HttpRequest.get(url).execute();
        } catch (Exception e) {
            DialogUtils.showException("初始化学校列表数据失败!", e);
            e.printStackTrace();
            return null;
        }
        JSONObject jsonData = JSONObject.parseObject(execute.body());
        JSONArray jsonArray = jsonData.getJSONArray("data");
        if (jsonArray.size() == 0) {
            DialogUtils.showException("初始化学校列表数据失败!", new RuntimeException("未查询到学校列表!"));
        }
        List<School> schoolList = jsonArray.toJavaList(School.class);
        for (School school : schoolList) {
            Integer schoolId = school.getId();
            String schoolName = school.getSchoolName();
            schoolMap.put(schoolName, schoolId);
        }
        return new ArrayList<String>(schoolMap.keySet());
    }

    /**
     * 获取学校列表数据
     *
     * @return
     */
    public List<String> getHttpBatchList() {
        batchMap.clear();
        String url = HttpConstant.BATCH_URL;
        HttpResponse execute = null;
        try {
            execute = HttpRequest.get(url).execute();
        } catch (Exception e) {
            DialogUtils.showException("初始化学期列表数据失败!", e);
            e.printStackTrace();
            return null;
        }
        JSONObject jsonData = JSONObject.parseObject(execute.body());
        JSONArray jsonArray = jsonData.getJSONArray("data");
        if (jsonArray.size() == 0) {
            DialogUtils.showException("初始化学期列表数据失败!", new RuntimeException("未查询到学期列表!"));
        }
        List<Batch> batchList = jsonArray.toJavaList(Batch.class);
        for (Batch batch : batchList) {
            Integer batchId = batch.getId();
            String batchName = batch.getBatchName();
            batchMap.put(batchName, batchId);
        }
        return new ArrayList<String>(batchMap.keySet());
    }

    /**
     * 初始化excel表格的数据
     *
     * @param event
     */
    public void initExcelData(ActionEvent event) {
        Object schoolNameObj = schoolComboBox.getValue();
        String schoolName = schoolNameObj.toString();
        if (StrUtil.isBlank(schoolName)) {
            schoolId = 0;
            ConfigUtil.deleteEntity(DbConstant.SCHOOL_NAME);
            ConfigUtil.deleteEntity(DbConstant.SCHOOL_ID_NAME);
        } else {
            Integer mapValue = schoolMap.get(schoolName);
            if (mapValue != null) {
                schoolId = mapValue;
                ConfigUtil.insertOrUpdateEntity(DbConstant.SCHOOL_NAME, schoolName);
                ConfigUtil.insertOrUpdateEntity(DbConstant.SCHOOL_ID_NAME, schoolId);

            }
        }
        Object batchNameObj = batchComboBox.getValue();
        String batchName = "";
        if (batchNameObj != null) {
            batchName = batchNameObj.toString();
            if (StrUtil.isNotBlank(batchName)) {
                Integer batchMapValue = batchMap.get(batchName);
                if (batchMapValue != null) {
                    batchId = batchMapValue;
                }
            }
        }
        log.warn("选择的学校:{},学校的ID:{}!", schoolName, schoolId);
        log.warn("选择的学期:{},学期的ID:{}!", batchName, batchId);
        Stage stage = (Stage) handlerBtn.getScene().getWindow();
        progressFrom = new ProgressFrom(stage);
        progressFrom.activateProgressBar();
        List<Map<String, Object>> collegeListMap = null;
        List<Map<String, Object>> majorListMap = null;
        List<Map<String, Object>> officeListMap = null;
        List<Map<String, Object>> teacherListMap = null;
        List<Map<String, Object>> clazzListMap = null;
        List<Map<String, Object>> studentListMap = null;
        List<Map<String, Object>> courseListMap = null;
        List<Map<String, Object>> courseStudentListMap = null;
        log.warn("数据处理第1步开始:准备初始化EXCEL转List<Map>,将EXCEL的行与列转换为List<Map>!");
        if (CollUtil.isNotEmpty(collegeFileList)) {
            try {
                collegeListMap = initMapData(collegeFileList);
                log.warn("院系EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                progressFrom.cancelProgressBar();
                DialogUtils.showException(e);
                return;
            }

        }
        if (CollUtil.isNotEmpty(majorFileList)) {
            try {
                majorListMap = initMapData(majorFileList);
                log.warn("专业EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                progressFrom.cancelProgressBar();
                DialogUtils.showException(e);
                return;
            }
        }
        if (CollUtil.isNotEmpty(officeFileList)) {
            try {
                officeListMap = initMapData(officeFileList);
                log.warn("教研室EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                DialogUtils.showException(e);
                progressFrom.cancelProgressBar();
                return;
            }
        }
        if (CollUtil.isNotEmpty(teacherFileList)) {
            try {
                teacherListMap = initMapData(teacherFileList);
                log.warn("教师EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                DialogUtils.showException(e);
                progressFrom.cancelProgressBar();
                return;
            }
        }
        if (CollUtil.isNotEmpty(clazzFileList)) {
            try {
                clazzListMap = initMapData(clazzFileList);
                log.warn("班级EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                DialogUtils.showException(e);
                progressFrom.cancelProgressBar();
                return;
            }
        }
        if (CollUtil.isNotEmpty(studentFileList)) {
            try {
                studentListMap = initMapData(studentFileList);
                log.warn("学生EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                DialogUtils.showException(e);
                progressFrom.cancelProgressBar();
                return;
            }
        }
        if (CollUtil.isNotEmpty(courseFileList)) {
            try {
                courseListMap = initMapData(courseFileList);
                log.warn("课程EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                DialogUtils.showException(e);
                progressFrom.cancelProgressBar();
                return;
            }
        }
        if (CollUtil.isNotEmpty(courseStuFileList)) {
            try {
                courseStudentListMap = initMapData(courseStuFileList);
                log.warn("学生选修课EXCEL转List<Map>完成!");
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
                DialogUtils.showException(e);
                progressFrom.cancelProgressBar();
                return;
            }
        }
        log.warn("数据处理第1步结束:初始化全部EXCEL转Map工作完成!");
        String configPathText = configPathField.getText();
        if (StrUtil.isBlank(configPathText)) {
            AlertUtil.showErrorAlert("请选择配置文件!");
            progressFrom.cancelProgressBar();
            return;
        } else {
            if (!FileUtil.exist(configPathText)) {
                AlertUtil.showErrorAlert("配置文件路径不正确!");
                progressFrom.cancelProgressBar();
                return;
            }
        }
        handlerExcelData(collegeListMap, majorListMap, officeListMap, teacherListMap, clazzListMap,
                studentListMap, courseListMap, courseStudentListMap, configPathText);
    }

    /**
     * 将excel数据读取成list
     *
     * @param fileList
     * @return
     */
    private List<Map<String, Object>> initMapData(List<File> fileList) {
        List<Map<String, Object>> listMap = new ArrayList<>();
        int headerRowNum = 1;
        String headerRowStr = headerRowIndex.getText();
        if (StrUtil.isNotBlank(headerRowStr)) {
            headerRowNum = Integer.parseInt(headerRowStr);
        }
        ExcelReader reader = null;
        for (File f : fileList) {
            reader = ExcelUtil.getReader(f);
            List<Map<String, Object>> tmpList = reader.read(headerRowNum - 1, headerRowNum, Integer.MAX_VALUE);
            listMap.addAll(tmpList);
        }
        if (reader != null) {
            reader.close();
        }
        return listMap;
    }

    /**
     * 处理excel数据
     *
     * @param collegeListMap
     * @param majorListMap
     * @param officeListMap
     * @param teacherListMap
     * @param clazzListMap
     * @param studentListMap
     * @param courseListMap
     * @param configPath
     */
    private void handlerExcelData(List<Map<String, Object>> collegeListMap,
                                  List<Map<String, Object>> majorListMap,
                                  List<Map<String, Object>> officeListMap,
                                  List<Map<String, Object>> teacherListMap,
                                  List<Map<String, Object>> clazzListMap,
                                  List<Map<String, Object>> studentListMap,
                                  List<Map<String, Object>> courseListMap,
                                  List<Map<String, Object>> courseStudentListMap,
                                  String configPath) {
        GenCodeVO genCodeVO = new GenCodeVO();
        genCodeVO.setDownAll(downAllCheckBox.isSelected());
        genCodeVO.setGenCollege(genCollegeCodeBox.isSelected());
        genCodeVO.setGenMajor(genMajorCodeBox.isSelected());
        genCodeVO.setGenOffice(genOfficeCodeBox.isSelected());
        genCodeVO.setGenClazz(genClazzCodeBox.isSelected());
        //核心方法
        HandlerResultData handlerResultData = new HandlerResultData();
        log.debug("创建数据处理完成下载的临时目录");
        String tmpdir = System.getProperty("java.io.tmpdir");
        String rootPath = tmpdir + File.separator + "changxianggu" + File.separator;
        try {
            handlerResultData.handlerSchoolData(collegeListMap, majorListMap, officeListMap, teacherListMap, clazzListMap,
                    studentListMap, courseListMap, courseStudentListMap, configPath, schoolId, batchId, genCodeVO);
            log.debug("数据处理第5步:弹出文件选择框,将压缩包写入用户选择的地址");
            FileChooser fileChooser = new FileChooser();
            FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Zip files (*.zip)", "*.zip");
            fileChooser.getExtensionFilters().add(extFilter);
            Stage s = new Stage();
            File file = fileChooser.showSaveDialog(s);
            progressFrom.cancelProgressBar();
            if (file == null) {
                return;
            }
            //文件已存在，则删除覆盖文件
            if (file.exists()) {
                file.delete();
            }
            String exportFilePath = file.getAbsolutePath();
            ZipUtil.zip(rootPath, exportFilePath);
            log.debug("数据处理第5步结束!");
            log.debug("数据处理第6步:删除临时目录文件和清理缓存!");
            //使用完成后删除临时目录
            FileUtil.del(rootPath);
            clearData();
            AlertUtil.showInfoAlert("处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            DialogUtils.showException(e);
        } finally {
            if (progressFrom != null) {
                progressFrom.cancelProgressBar();
            }
        }
    }

    /**
     * 清除数据
     */
    private void clearData() {
        if (collegeFilePath != null) {
            collegeFilePath.setText("");
            collegeFileList = null;
        }
        if (officeFilePath != null) {
            officeFilePath.setText("");
            officeFileList = null;
        }
        if (majorFilePath != null) {
            majorFilePath.setText("");
            majorFileList = null;
        }
        if (clazzFilePath != null) {
            clazzFilePath.setText("");
            clazzFileList = null;
        }
        if (studentFilePath != null) {
            studentFilePath.setText("");
            studentFileList = null;
        }
        if (teacherFilePath != null) {
            teacherFilePath.setText("");
            teacherFileList = null;
        }
        if (courseFilePath != null) {
            courseFilePath.setText("");
            courseFileList = null;
        }
        if (courseStuFilePath != null) {
            courseStuFilePath.setText("");
            courseStuFileList = null;
        }
    }

    private void initExcelRootPath(File file) {
        if (file != null) {
            File parent = FileUtil.getParent(file, 1);
            if (parent != null) {
                excelRootDir = parent;
                String excelRootDirPath = parent.getAbsolutePath();
                ConfigUtil.insertOrUpdateEntity(DbConstant.EXCEL_FILE_DIR_NAME, excelRootDirPath);

            }
        }
    }

    /**
     * 选择配置文件
     *
     * @param actionEvent
     */
    public void choseConfigFile(ActionEvent actionEvent) {
        Stage stage = (Stage) choseConfigBtn.getScene().getWindow();
        File file = openChoseFile(stage);
        if (file != null) {
            String absolutePath = file.getAbsolutePath();
            configPathField.setText(absolutePath);
            ConfigUtil.insertOrUpdateEntity(DbConstant.CONFIG_FILE_PATH_NAME, absolutePath);
        }
    }

    /**
     * 初始化是否选择下载平台数据
     *
     * @param actionEvent
     */
    public void initDownAllCheck(ActionEvent actionEvent) {
        boolean selected = downAllCheckBox.isSelected();
        if (selected) {
            ConfigUtil.insertOrUpdateEntity(DbConstant.IS_DOWN_ALL_NAME, 1);
        } else {
            ConfigUtil.insertOrUpdateEntity(DbConstant.IS_DOWN_ALL_NAME, 0);
        }
    }

    /**
     * 打开院系excel文件选择框
     *
     * @param event
     */
    public void choseCollegeFile(ActionEvent event) {
        Stage stage = (Stage) choseCollegeBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            collegeFilePath.setText(joinFileName(fileList));
            collegeFileList = fileList;
            log.debug("选择院系EXCEL,文件数量:{}", fileList.size());
        } else {
            collegeFilePath.setText("");
            collegeFileList = null;
            log.debug("打开了院系EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开教研室excel文件选择框
     *
     * @param event
     */
    public void choseOfficeFile(ActionEvent event) {
        Stage stage = (Stage) choseOfficeBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            officeFilePath.setText(joinFileName(fileList));
            officeFileList = fileList;
            log.debug("选择教研室EXCEL,文件数量:{}", fileList.size());
        } else {
            officeFilePath.setText("");
            officeFileList = null;
            log.debug("打开了教研室EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开专业excel文件选择框
     *
     * @param event
     */
    public void choseMajorFile(ActionEvent event) {
        Stage stage = (Stage) choseMajorBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            majorFilePath.setText(joinFileName(fileList));
            majorFileList = fileList;
            log.debug("选择专业EXCEL,文件数量:{}", fileList.size());
        } else {
            majorFilePath.setText("");
            majorFileList = null;
            log.debug("打开了专业EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开班级excel文件选择框
     *
     * @param event
     */
    public void choseClazzFile(ActionEvent event) {
        Stage stage = (Stage) choseClazzBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            clazzFilePath.setText(joinFileName(fileList));
            clazzFileList = fileList;
            log.debug("选择班级EXCEL,文件数量:{}", fileList.size());
        } else {
            clazzFilePath.setText("");
            clazzFileList = null;
            log.debug("打开了班级EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开学生excel文件选择框
     *
     * @param event
     */
    public void choseStudentFile(ActionEvent event) {
        Stage stage = (Stage) choseStudentBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            studentFilePath.setText(joinFileName(fileList));
            studentFileList = fileList;
            log.debug("选择学生EXCEL,文件数量:{}", fileList.size());
        } else {
            studentFilePath.setText("");
            studentFileList = null;
            log.debug("打开了学生EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开教师excel文件选择框
     *
     * @param event
     */
    public void choseTeacherFile(ActionEvent event) {
        Stage stage = (Stage) choseTeacherBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            teacherFilePath.setText(joinFileName(fileList));
            teacherFileList = fileList;
            log.debug("选择教师EXCEL,文件数量:{}", fileList.size());
        } else {
            teacherFilePath.setText("");
            teacherFileList = null;
            log.debug("打开了教师EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开课程excel文件选择框
     *
     * @param event
     */
    public void choseCourseFile(ActionEvent event) {
        Stage stage = (Stage) choseCourseBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            courseFilePath.setText(joinFileName(fileList));
            courseFileList = fileList;
            log.debug("选择课程EXCEL,文件数量:{}", fileList.size());
        } else {
            courseFilePath.setText("");
            courseFileList = null;
            log.debug("打开了教师EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }

    /**
     * 打开学生选修课excel文件选择框
     *
     * @param event
     */
    public void choseCourseStuFile(ActionEvent event) {
        Stage stage = (Stage) choseCourseStuBtn.getScene().getWindow();
        List<File> fileList = openMultiFile(stage);
        if (CollUtil.isNotEmpty(fileList)) {
            initExcelRootPath(fileList.get(0));
            courseStuFilePath.setText(joinFileName(fileList));
            courseStuFileList = fileList;
            log.debug("选择学生选修课程EXCEL,文件数量:{}", fileList.size());
        } else {
            courseStuFilePath.setText("");
            courseStuFileList = null;
            log.debug("打开了教师EXCEL文件选择框,但是没有选择EXCEL文件");
        }
    }
}
